﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using GameEngine.Worlds;
using GameEngine.Interactors;

using GameEngineSL.SilverlightCompatibility;

namespace WebGuiSL
{
    public partial class MainPage : UserControl
    {
        private Renderer renderer;
        private DateTime lastTick;

        private World myWorld;
        private IPointerInteractor interactor;

        public MainPage()
        {
            InitializeComponent();

            renderer = new Renderer(MainCanvas);

            myWorld = new World();
            Unit unit = new Unit();
            myWorld.Units.Add(unit);

            interactor = new BattleMapInteractor(myWorld);

            lastTick = DateTime.Now;
            
            // Add an EventHandlers.
            CompositionTarget.Rendering += RenderFrame;
            this.MouseMove += MainPage_MouseMove;
        }

        private void RenderFrame(object sender, EventArgs e)
        {
            // Hold the elapsed time after the last call to this method.
            TimeSpan elapsedTime = DateTime.Now - lastTick;
            lastTick = DateTime.Now;
            
            myWorld.Tick((float)elapsedTime.TotalSeconds);
            
            renderer.Render(myWorld);
        }

        private void MainPage_MouseMove(object sender, MouseEventArgs e)
        {
            Point point = e.GetPosition(MainCanvas);
            PointF pointF = point;
            interactor.OnMouseMove(pointF);
        }
    }
}
